﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using BusinessLayer;
using EntityModels;
using System.Data.Entity;

namespace PresentationLayer
{
    public partial class CreateEvent : AbstractPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                CheckCookie();
            }
            catch (Exception ex)
            {
                this.debugMessage.InnerHtml += ex.Message;
            }
        }

        protected void AddLocation(object sender, EventArgs e)
        {
            List<Location> locations = new List<Location>();

            if (Session["Locations"] != null)
            {
                locations = (List<Location>)(Session["Locations"]);
            }


            locations.Add(new Location(locationName.Text, country.Text, city.Text, addressA.Text + " " + addressB.Text));
            Session["Locations"] = locations;

            Session.Timeout = 10;

            viewLocations();
        }

        protected void AddDate(object sender, EventArgs e)
        {
            List<DateTime> dates = new List<DateTime>();
            if (Session["Dates"] != null)
            {
                dates = (List<DateTime>)(Session["Dates"]);
            }
             
            dates.Add(parseToDateTime(date.Text));
            Session["Dates"] = dates;
            Session.Timeout = 10;

            viewDates();
        }

        protected void AddTime(object sender, EventArgs e)
        {
            List<TimeSpan> times = new List<TimeSpan>();
            if (Session["Times"] != null)
            {
                times = (List<TimeSpan>)(Session["Times"]);
            }
            times.Add(parseToTimeSpan(time.Text));
            Session["Times"] = times;
            Session.Timeout = 10;

            viewTimes();
        }

        private void viewLocations()
        {
            if (Session["Locations"] != null)
            {
                List<Location> locations = (List<Location>)(Session["Locations"]);
                int i = 0;
                if (locations.Count > 1) { endvotationdiv.Visible = true; }
                viewLoc.InnerHtml = "";
                StringBuilder sb = new StringBuilder("<table class=\"AddedOptions\"><tr>");

                foreach (Location l in locations)
                {
                    i++;

                    sb.Append("<td class=\"OptionNumber\"><p>#" + i + "</p></td>");
                    sb.Append("<td class=\"OptionValues\"><p>");
                    sb.Append(l.locationplace + "<br />");
                    sb.Append(l.address + "<br />");
                    sb.Append(l.city + ", ");
                    sb.Append(l.country + "<br />");
                    sb.Append("</p></td><td>&nbsp;</td>");

                    if (i % 3 == 0) sb.Append("</tr><tr>");
                }

                sb.Append("</tr></table>");

                viewLoc.InnerHtml += sb.ToString();
            }
        }

        private void viewDates()
        {
            if (Session["Dates"] != null)
            {
                List<DateTime> dates = (List<DateTime>)(Session["Dates"]);
                int i = 0;
                if (dates.Count > 1) { endvotationdiv.Visible = true; }
                viewD.InnerHtml = "";
                StringBuilder sb = new StringBuilder("<table class=\"AddedOptions\"><tr>");

                foreach (DateTime d in dates)
                {
                    i++;

                    sb.Append("<td class=\"OptionNumber\"><p>#" + i + "</p></td>");
                    sb.Append("<td class=\"OptionValues\"><p>");
                    sb.Append(d.ToShortDateString());
                    sb.Append("</p></td><td>&nbsp;</td>");

                    if (i % 4 == 0) sb.Append("</tr><tr>");
                }

                sb.Append("</tr></table>");
                viewD.InnerHtml = sb.ToString();
            }
        }

        private void viewTimes()
        {
            if (Session["Times"] != null)
            {
                List<TimeSpan> times = (List<TimeSpan>)(Session["Times"]);
                int i = 0;
                if (times.Count > 1) { endvotationdiv.Visible = true; }
                viewT.InnerHtml = "";
                StringBuilder sb = new StringBuilder("<table class=\"AddedOptions\"><tr>");

                foreach (TimeSpan t in times)
                {
                    i++;

                    sb.Append("<td class=\"OptionNumber\"><p>#" + i + "</p></td>");
                    sb.Append("<td class=\"OptionValues\"><p>");
                    sb.Append(t.ToString());
                    sb.Append("</p></td><td>&nbsp;</td>");

                    if (i % 4 == 0) sb.Append("</tr><tr>");
                }

                sb.Append("</tr></table>");
                viewT.InnerHtml = sb.ToString();
            }
        }

        protected void Create(object sender, EventArgs e)
        {
            bool valid = true;

            if (Session["Locations"] == null)
            {
                valid = false;
                viewLoc.InnerHtml = "*Please add at at least one location.";
            }

            if (Session["Dates"] == null)
            {
                valid = false;
                viewD.InnerHtml = "*Please add at at least one date.";
            }

            if (Session["Times"] == null)
            {
                valid = false;
                viewT.InnerHtml = "*Please add at at least one time.";
            }

            if (valid)
            {

                HttpCookie cookie = Request.Cookies["eventnet"];
                User us = GetUserFromCookie();
                Event ev = new Event();
                List<Location> locations = (List<Location>)(Session["Locations"]);
                List<TimeSpan> times = (List<TimeSpan>)(Session["Times"]);
                List<DateTime> dates = (List<DateTime>)(Session["Dates"]);

                ev.name = eventName.Text;
                ev.eidmember = us.idmember;
                if (endvotationdiv.Visible == true)
                {
                    try
                    {
                        ev.voteenddate = parseToDateTime(endofvotations.Text);
                    }
                    catch
                    {
                        ev.voteenddate = DateTime.Today;
                    }
                }
                else {
                    ev.voteenddate = DateTime.Today;
                }

                if (Visiblity_Private.Checked)
                {
                    ev.visibility = false;
                }
                else
                {
                    ev.visibility = true;
                }

                bool result = Controller.Instance.CreateEvent(ev, locations, dates, times);
                if (result)
                {
                    Session.Clear();
                    Response.Redirect("Profile.aspx");

                }
            }
        }

        //Recibe a date like: yyyy-mm-dd
        public static DateTime parseToDateTime(String date)
        {
            string[] numbers = date.Split('-');
            Console.WriteLine();
            DateTime d = new DateTime(Convert.ToInt32(numbers[0]), Convert.ToInt32(numbers[1]), Convert.ToInt32(numbers[2]));
            return d;
        }
        //Recibe a time like: hh:mm
        public static TimeSpan parseToTimeSpan(String date)
        {
            string[] numbers = date.Split(':');
            TimeSpan t = new TimeSpan(Convert.ToInt32(numbers[0]), Convert.ToInt32(numbers[1]), 00);
            return t;
        }
    }
}